package com.atguigu.ucenter.service.impl;

import com.atguigu.base.ExceptionHnadler.CustomException;
import com.atguigu.ucenter.entity.Member;
import com.atguigu.ucenter.mapper.MemberMapper;
import com.atguigu.ucenter.service.MemberService;
import com.atguigu.ucenter.vo.LoginVo;
import com.atguigu.ucenter.vo.RegisterVo;
import com.atguigu.utils.JwtUtils;
import com.atguigu.utils.MD5;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.ConditionalFormattingThreshold;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author ld
 * @since 2022-08-21
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Resource
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public String login(LoginVo member) {
        String mobile = member.getMobile();
        String password = member.getPassword();
        if(StringUtils.isEmpty(mobile)){
            throw new CustomException(20001,"手机号为空,登录失败");
        }
        if(StringUtils.isEmpty(password)){
            throw new CustomException(20001,"密码为空,登录失败");
        }
        LambdaQueryWrapper<Member> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Member::getMobile,mobile);
        Member one = baseMapper.selectOne(queryWrapper);
        if(one==null){
            throw new CustomException(20001,"这个号码没注册,登录失败");
        }
        if(!MD5.encrypt(password).equals(one.getPassword())){
            throw new CustomException(20001,"密码不存在,登录失败");
        }
        if(one.getIsDisabled()){
             throw new CustomException(20001,"手机号被禁用,登录失败");
        }
        String jwtToken = JwtUtils.getJwtToken(one.getId(), one.getNickname());
        return jwtToken;
    }

    @Override
    public boolean register(RegisterVo registerVo) {
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();
        if(StringUtils.isEmpty(mobile)){
            throw new CustomException(20001,"手机号为空,注册失败");
        }
        if(StringUtils.isEmpty(password)){
            throw new CustomException(20001,"密码为空,注册失败");
        }
        if(StringUtils.isEmpty(code)){
            throw new CustomException(20001,"验证码为空,注册失败");
        }
        //手机号是否被注册
        LambdaQueryWrapper<Member> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Member::getMobile,mobile);
        Integer count = baseMapper.selectCount(queryWrapper);
        if(count>=1){
            throw new CustomException(20001,"手机号已经被注册,注册失败");
        }
        String code1 = redisTemplate.opsForValue().get(mobile);
        if(!code.equals(code1)){
            throw new CustomException(20001,"验证码出错,注册失败");
        }

        Member member=new Member();
        member.setMobile(mobile);
        member.setPassword(MD5.encrypt(mobile));
        member.setNickname(registerVo.getNickname());
        member.setIsDisabled(false);
        member.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        baseMapper.insert(member);
        return true;
    }

    //根据openid查询
    @Override
    public Member getOpenIdMember(String openid) {

        LambdaQueryWrapper<Member> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Member::getOpenid,openid);
        Member member = baseMapper.selectOne(queryWrapper);
        return member;

    }
    //获取每天的注册人数
    @Override
    public Integer getCountRegister(String day) {
        return baseMapper.getCountRegister(day);
    }

}
